package com.xuecheng.media.controller;

import com.xuecheng.commons.model.dto.*;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.VodUploadVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.Media;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService  mediaService;

    /**
     * 获取阿里云上传凭证
     * POST: /vod-token
     * @param dto VodUploadDto
     * @return ResponseResult<VodUploadVo>
     */
    @PostMapping("/vod-token")
    public ResponseResult createToken(@RequestBody VodUploadDto dto) throws Exception {
        return mediaService.createToken(dto);
    }

    /**
     * 刷新 凭证
     * POST: /refresh-vod-token/{videoId}
     * @param videoId 视频id
     * @return ResponseResult<VodUploadVo>
     */
    @GetMapping("/refresh-vod-token/{videoId}")
    public ResponseResult refreshToken(@PathVariable("videoId") String videoId) throws Exception {
        return mediaService.refreshToken(videoId);
    }

    /**
     * 保存媒体信息
     * @param dto VodUploadDto
     * POST
     * @return ResponseResult<VodUploadVo>
     * @throws Exception 内部异常
     */
    @PostMapping
    public ResponseResult saveMedia(@RequestBody MediaDto dto) throws Exception {
        return  mediaService.saveMedia(dto);
    }

    /**
     * 媒资 分页查询 列表
     * POST /list
     * @param pageRequest 分页对象{ pageNo pageSize}
     * @param dto MediaQueryDto
     * @return ResponseResult<pageResponseResult>
     */
    @PostMapping("/list")
    public ResponseResult findMediaList(PageRequest pageRequest , @RequestBody MediaQueryDto dto){
        return mediaService.findMediaList(pageRequest,dto);
    }

    /**
     * 根据id 查询媒体资料
     * GET /{mediaId}
     * @param mediaId 媒资 id
     * @return MediaVo
     */
    @GetMapping("/{mediaId}")
    public ResponseResult findById(@PathVariable("mediaId") Long mediaId){
        Media media = mediaService.getById(mediaId);
        MediaVo mediaVo = BeanHelper.copyProperties(media, MediaVo.class);
        return  ResponseResult.okResult(mediaVo);
    }

    /**
     * 媒资审核
     * @param dto MediaAuditDto
     * @return null
     */
    @PutMapping("/audit")
    public ResponseResult audit(@RequestBody MediaAuditDto dto){
        return mediaService.audit(dto);
    }
    /**
     * 媒体资料预览
     * @param mediaId 媒体资料id
     * @return 路径
     */
    @GetMapping("/preview/{mediaId}")
    public ResponseResult preview(@PathVariable("mediaId") Long mediaId){
        Media media = mediaService.getById(mediaId);
        String url = media.getUrl();
        return  ResponseResult.okResult(url);
    }

    /**
     * 媒资删除
     * @param mediaId 媒资id
     * @return vo
     */
    @DeleteMapping("/{mediaId}")
    public ResponseResult deleteById(@PathVariable("mediaId") Long mediaId){
        return mediaService.deleteById(mediaId);
    }

    /**
     * 根据id查询媒资地址
     * @param mediaId 媒资id
     * @return ResourceVo
     */
    @GetMapping("/media-url/{mediaId}")
    public ResponseResult findMediaUrl(@PathVariable("mediaId") Long mediaId) throws Exception {
        return mediaService.findMediaUrl(mediaId);
    }
}

